package com.example.jwtdemo.controllers;

import com.example.core.libs.JSONResult;
import com.example.core.utils.JwtUtils;
import com.example.core.utils.RedisUtil;
import com.example.jwtdemo.annotation.Login;
import com.example.jwtdemo.interceptor.AuthorizationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class TestController {
    @Autowired
    JwtUtils jwtUtils;
    @Autowired
    RedisUtil redisUtil;

    @RequestMapping("/login")
    public JSONResult login() {
        int userId = 1;
        String token = jwtUtils.generateToken(String.valueOf(userId));
        redisUtil.hmSet(AuthorizationInterceptor.USER_REDIS_LOGIN, String.valueOf(userId), token);
        return new JSONResult().put("token", token);
    }

    @RequestMapping("/get")
    public JSONResult get() {
        return new JSONResult();
    }

    @Login
    @RequestMapping("/app/get")
    public JSONResult appGet(HttpServletRequest request) {
        String userId = String.valueOf(request.getAttribute("userId"));
        return new JSONResult().put("userId", userId);
    }
}
